package com.haiyi.software.app.basic;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;

import android.os.Bundle;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.L;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public abstract class ActivityBasicFeedback extends ActivityBasic {

	private String str_requestName = "recommend.php";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
	}

	/**
	 * 反馈
	 * 
	 * @param str_type
	 * @param str_content
	 */
	protected void recommend(String str_type, String str_content) {

		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();

		if (myInfo != null) {

			Map<String, Object> validator = new HashMap<String, Object>();

			validator.put("uid", myInfo.getUid());
			validator.put("uname", myInfo.getUname());
			validator.put("pwd", myInfo.getPwd());
			validator.put("uguid", myInfo.getUguid());

			Map<String, Object> body = new HashMap<String, Object>();

			body.put("rCname", str_type);
			body.put("rDesc", str_content);

			Map<String, Object> ags = new HashMap<String, Object>();

			ags.put("validator", validator);
			ags.put("body", body);

			Gson gson = new Gson();
			String s = gson.toJson(ags);

			L.i("recommend=" + s);

			requestNetworkData(s);

		}

	}

	@Override
	protected void requestNetworkData(Object... args) {
		// TODO 自动生成的方法存根
		String str_requestURL = str_server + str_requestName;
		AsyncHttpClient client = MainApplication.getInstance()
				.getAsyncHttpClient();
		RequestParams params = new RequestParams();
		params.add("args", args[0].toString());
		watingDialog.show();
		client.post(str_requestURL, params, responseHandler);
	}

	private AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			watingDialog.hide();

			try {
				String str_jsonReturn = new String(responseBody, "UTF-8");

				L.i("str_jsonReturn=" + str_jsonReturn);

				JsonParser parser = new JsonParser();

				JsonObject jsonObj_return = parser.parse(str_jsonReturn)
						.getAsJsonObject();

				requestFinish(jsonObj_return);

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				requestError();
			}
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {
			watingDialog.hide();
			requestError();
		}
	};
}
